package com.bosssoft.hr.train.j2se.util;

import com.bosssoft.hr.train.j2se.pojo.entity.User;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;


import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class UtilsDemo {
    /**
     * 创建测试数据
     * @return 整数数组列表
     */
    private List<Integer> createList(){
        List<Integer> integerList = new ArrayList<>();
        // 初始化终止
        Random random=new Random();
        for(int i=0;i<10;i++){
            integerList.add(random.nextInt(100));//生成100以内数字
        }
        return integerList;
    }
    /**
     *method1
     * jdkUtil Collections
     * sort()
     */
    public void method1(){
        List<Integer> list = createList();
        Collections.sort(list);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    /**
     * method2
     * Apache CollectionUtils
     * isEmpty()
     * Exist()
     * AddAll()
     */
    public void method2(){
        List<Integer> list = createList();
        System.out.println("是否为空："+CollectionUtils.isEmpty(list));
        String[] langs ={"java","python","c++"};
        CollectionUtils.exists(Arrays.asList(langs), new Predicate<String>() {
            @Override
            public boolean evaluate(String s) {
                return "python".equalsIgnoreCase(s);
            }
        });
        String[] langs2={"java2","python2","c++2"};
        CollectionUtils.addAll(Arrays.asList(langs),langs2);
    }

    /**
     * method3
     * ArrayListUtils
     * asList()
     * sort()
     * copyOf()
     * binarySearch()
     */
    public void method3(){
        Integer[] num = new Integer[]{9,5,1,7,6,3};
        List<Integer> list = Arrays.asList(num);
        Arrays.sort(num);
        Integer[] copyNum = Arrays.copyOf(num,num.length);
        Arrays.binarySearch(num,5);

    }

    /**
     * method4
     * Apache FileUtils
     * listFiles()
     */
    public void method4(){
        String pathname = "";
        Collection files = FileUtils.listFiles(new File(pathname),new String[]{"*.java"},true);

    }

    /**
     * method5
     * Apache StringUtils
     * isEmpty()
     */
    public void method5(){
        StringUtils.isEmpty("");
        String str = null;
        StringUtils.isEmpty(str);

    }

    /**
     * method6
     * Apache BeanUtils
     * copyProperties()
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void method6() throws InvocationTargetException, IllegalAccessException {
        User user = new User(1L,"BossSoft","用户");
        User copyUser = new User();
        BeanUtils.copyProperties(user,copyUser);
    }

    /**
     * method7
     * Apache DigestUtils
     * md5()
     */
    public void method7(){
        String password = "123456789";
        DigestUtils.md5(password);

    }

    /**
     * method8
     * Apache DigestUtils
     * sha256()
     */
    public void method8(){
        String password = "123456789";
        DigestUtils.sha256(password);

    }

}
